home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 4035 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: doc.ic.ac.uk!not-for-mail
  2. From: mdf@doc.ic.ac.uk (Martin Frost)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: Need help with strings in SAS/C
  5. Date: 16 Feb 1996 13:48:44 -0000
  6. Organization: Dept. of Computing, Imperial College, University of London, UK.
  7. Distribution: world
  8. Message-ID: <4g21vs$8ar@motmot.doc.ic.ac.uk>
  9. References: <4fehq5$keg@news.unicomp.net>
  10. Reply-To: mdf@doc.ic.ac.uk (Martin Frost)
  11. NNTP-Posting-Host: motmot.doc.ic.ac.uk
  12. Keywords: SAS C
  13. X-Newsreader: mxrn 6.18-23
  14.  
  15.  
  16. In article <4fehq5$keg@news.unicomp.net>, rays@conline.com (Ray Schmalzl) writes:
  17.  
  18. >void main()
  19.  ^^^^
  20.  
  21. main() returns an int.
  22.  
  23. >   char CharOne, CharTwo[]="012345";
  24.  
  25. This should be
  26.  
  27.     char CharOne[2], CharTwo[]="012345";
  28.  
  29. >   CharOne = getch();        // assume typing in a digit here
  30.  
  31.     CharOne[0] = getch();
  32.     CharOne[1] = 0;
  33.  
  34. The variable definition "char CharOne" simply defines enough space for 1 char.
  35. The function atoi() takes a *string*, which is a pointer to memory containing
  36. zero or more chars and then a null char.
  37.  
  38. The declaration "char CharTwo[]" (equivalent to "char *CharTwo") defines a
  39. string, which in this case is pre-initialised to point to 7 chars' worth of
  40. memory containing the hex values 0x30,0x31,0x32,0x33,0x34,0x35,0x00. Because
  41. of this, atio() will work correctly on CharTwo.
  42.  
  43. If you want to convert a single digit integer read with getch() then you must
  44. create a dummy string consisting of the character and then a zero byte. Since
  45. a string in C is exactly the same as an array of char with the last char zero,
  46. this is what we do above.
  47.  
  48. >   printf("%i\n",atoi(CharOne)) ;
  49.              ^
  50. shouldn't that be %d?
  51.  
  52. >   printf("%c\t",CharTwo[3]) ;
  53.  
  54. this line is correct; CharTwo[3] has type char.
  55.  
  56. >   printf("%i\n",atoi (CharTwo[3]) ;
  57.  
  58. this line will also cause a typeclash error, as atoi needs a pointer to chars.
  59.  
  60. >Is there any way to do what I'm trying to do without declaring that extra
  61. >variable?
  62.  
  63. The *easiest* way of converting a single digit to its numerical value is to
  64. do:
  65.  
  66.     int v = (int)getch();
  67.     if(v>='0' && v<='9') v-='0';
  68.     else
  69.         {
  70.         /* char was not a digit */
  71.         }
  72.  
  73. >And what is the deal with that "ecpecting char const * found 
  74. >char" error?
  75.  
  76. It means that atoi() needs a pointer (the "char const *") and got a char.
  77.  
  78. Martin
  79.